/**
 * Created by Administrator on 2017/5/10 0010.
 */
checkCookie();
var mid = $.cookie('mid');
var token = $.cookie('token');
var card_no = location.search.split("=")[1];
var money = $('.money span').html();
var m_id = getQueryString('m_id');
getData({url: paylog, data: {mid: mid, token: token, m_id:m_id,card_no: card_no}}, function (data) {
    var moneyhtml = template('money', data);
    $('.money').html(moneyhtml);
});
var config = '';

$('.footer').on('touchstart', function () {
    var cash = $('input').val();
    if (cash < 1) {
        $('.error').html('金额不得小于1').show();
        return false
    }
    getData({url: balance, data: {mid: mid, token: token, money: cash, m_id:m_id,card_no: card_no}}, function (data) {
        if (data.state) {
            config = data.payconfig;
            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
            } else {
                onBridgeReady();
            }
        } else {
            creatLayout(data.msg);
        }
    })
});
function onBridgeReady() {
    if (typeof WeixinJSBridge === 'undefined') {
        alert('请在微信页打开页面！');
        return false;
    }
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest',
        {
            "appId": config.appId,     //公众号名称，由商户传入
            "timeStamp": config.timeStamp,         //时间戳，自1970年以来的秒数
            "nonceStr": config.nonceStr, //随机串
            "package": config.package,
            "signType": "MD5",         //微信签名方式：
            "paySign": config.paySign //微信签名
        },
        function (res) {
            switch (res.err_msg) {
                case 'get_brand_wcpay_request:cancel':
                    alert('用户取消支付！');
                    history.go(-1);
                    break;
                case 'get_brand_wcpay_request:fail':
                    alert('支付失败！（' + res.err_desc + '）');
                    break;
                case 'get_brand_wcpay_request:ok':
                    //跳转成功页
                    window.location.href = 'charge_success.html';
                    break;
                default:
                    alert(JSON.stringify(res));
                    break;
            }
        })
}

